/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAReservationCell.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 30/08/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAReservationCell.h"
#import "BAUtils.h"
#import "BAPatronReservationsViewController.h"

@interface BAReservationCell ()

@end

@implementation BAReservationCell

-(void)awakeFromNib
{
    BA_LABEL_SET_FONT_LIGHT(self.titleLabel);
    BA_LABEL_SET_FONT_LIGHT(self.dateLabel);
    BA_LABEL_SET_FONT_LIGHT(self.statusLabel);
    BA_LABEL_SET_FONT_LIGHT(self.queueNumberLabel);
    BA_LABEL_SET_FONT_LIGHT(self.queueLabel);
    BA_LABEL_SET_FONT_LIGHT(self.deleteReservationButton.titleLabel);

}

- (IBAction)deleteReservationButtonClicked:(id)sender {
    self.deleteReservationButton.hidden=YES;
    [self.loadingIndicator startAnimating];
    [self.parentReservationsController deleteReservation:theReservation];
}

-(void)setReservation:(BAReservation *)reservation
{
    theReservation = reservation;
    
    NSString* titlestring = [NSString stringWithFormat:@"%@%@%@",
                             theReservation.title,
                             ([theReservation.title length]>0 && [theReservation.note length]>0) ? @", " : @"",
                             theReservation.note
                             ];
    
    self.titleLabel.text = titlestring;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"da_DK"]];
    [dateFormatter setDateFormat:@"d. MMMM yyyy"];

    if(reservation.creators.count > 0) {
        self.statusLabel.text = [[NSString stringWithFormat:@"AF %@", [reservation.creators objectAtIndex:0]] uppercaseString];
    } else {
        self.statusLabel.text = @"";
    }
    
    if (theReservation.isReady) {
        NSDate* pickUpExpireDate = [NSDate dateWithTimeIntervalSince1970:theReservation.pickUpExpireDate];
        
        self.dateLabel.text = [NSString stringWithFormat:@"Hentes senest: %@, %@",[dateFormatter stringFromDate:pickUpExpireDate], theReservation.pickUpBranch];
        self.statusLabel.text = [NSString stringWithFormat:@"Nummer: %@, %@ (%@)", theReservation.pickUpNo, theReservation.status, theReservation.note];
    } else {
        
        if (theReservation.queueNo>0) {
            NSString *paddedStr = [NSString stringWithFormat:@"%02d", theReservation.queueNo];
            self.queueNumberLabel.text = paddedStr;
//            self.dateLabel.text = [NSString stringWithFormat:@"Kønummer: %d", theReservation.queueNo];
        } else {
            self.dateLabel.text = @"";
        }
//        self.statusLabel.text = [NSString stringWithFormat:@"%@ (%@)", theReservation.status, theReservation.note];
        
        if(reservation.creators.count > 0) {
            self.statusLabel.text = [[NSString stringWithFormat:@"AF %@", [reservation.creators objectAtIndex:0]] uppercaseString];
        } else {
            self.statusLabel.text = @"";
        }
    }
    
    if (theReservation.deleteReservationRpcPending) {
        self.deleteReservationButton.hidden=YES;
        [self.loadingIndicator startAnimating];
        
    } else {
        self.deleteReservationButton.hidden=NO;
        [self.loadingIndicator stopAnimating];
    }

    
}

@end
